package yl.hs.bmipfull.service.material.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.material.ISupplierDao;
import yl.hs.bmipfull.pojo.material.SupplierEntity;
import yl.hs.bmipfull.service.material.ISupplierService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.List;
import java.util.Map;

/**
 * 作用:   <br/>
 * 创建时间: 2021-05-18 16:22 <br/>
 *
 * @author 吴高丰
 */
@Service
public class SupplierServiceImpl extends PurchaseBaseService implements ISupplierService {
    @Autowired
    ISupplierDao dao;

    @Override
    public PageResult<SupplierEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(SupplierEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<SupplierEntity> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(),SupplierEntity.class);
        pageQuery.getQuery().put("unitSNArray", getCompanySNList());
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery(),filters));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery(),filters));
        return result;
    }

    @Override
    public SupplierEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    public List<SupplierEntity> findByName(String supplierName) {
        return dao.findByName(supplierName);
    }

    @Transactional
    public Integer saveSupplier(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        SupplierEntity entity;
        if (Tl.isEmpty(sn)) {
            //添加供应商
            entity = new SupplierEntity();
            Tl.getChangeLog(entity, model);
            if (findByName(entity.getSupplierName()).size() > 0) {
                return -1;
            }
            ;
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(SupplierEntity.class));
            entity.setOrganizationCode(getDepartSN());
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入供应商信息", Tl.toJson(entity), SupplierEntity.class);
            }
        } else {
            entity = findBySn(sn);
            var backJson = entity.toJson();
            var changeLog = Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, changeLog, backJson, SupplierEntity.class);
            }
        }
        if (result > 0) {
            saveGS(
                    entity.getSn(),
                    entity.getSupplierName(),
                    String.join(",",
                            entity.getSupplierName(),
                            entity.getAddress(),
                            entity.getContactName(),
                            entity.getContactPhone(),
                            entity.getRemark()
                    ),
                    entity,
                    HsApp.getCurrent() != null ? HsApp.getCurrent().getUserSN() : null,
                    entity.getOrganizationCode()
            );
        }
        return result;
    }

    @Transactional
    public Integer delete(String[] sn) {
        var result = dao.changeStateBySn(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, SupplierEntity.class);
            deleteGs(sn);
        }
        return result;
    }

    @Override
    public List<SupplierEntity> queryListAll() {
        return dao.queryListAll();
    }

    @Override
    public Integer changeStateBySn(String[] snArray, short state) {
        int result=dao.changeStateBySn(snArray,state);
        if (result > 0) {
            forWriteLog(snArray,state);
        }
        return result;
    }
}
